package cn.edu.ujn.shi.controller;

import cn.edu.ujn.shi.dao.Complain;
import cn.edu.ujn.shi.dao.UserType;
import cn.edu.ujn.shi.service.IUserTypeService;
import cn.hutool.core.date.DateUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * 用户表 前端控制器
 *
 * @author YSJ
 * @since 2022-03-03
 */
@Controller
public class UserTypeController {

    @Autowired
    IUserTypeService iUserTypeService;

    @RequestMapping("/userTypeList")
    public String userTypeList(HttpServletRequest request, HttpServletResponse response) {
        String type = request.getParameter("type");
        List<UserType> list = iUserTypeService.getType(type);
        request.setAttribute("list", list);
        return "UserTypeAdminList";
    }

    @RequestMapping("/addUserType")
    public String addComplain() {
        return "UserTypeAdd";
    }

    @RequestMapping("/updateUserType")
    public String updateComplain(HttpServletRequest request, HttpServletResponse response) {
        request.setAttribute("id", request.getParameter("id"));
        UserType userType = iUserTypeService.getById(request.getParameter("id"));
        //获取请求中的id参数，然后将这个参数传递给iUserTypeService.getById 方法，以便进行后续的用户类型查询和处理。
        request.setAttribute("type", userType.getType());
        return "UserTypeAdd";
    }

    @RequestMapping("addUserTypeServlet")
    public void addUserTypeServlet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession hs = request.getSession();//获取HttpSession 接口管理的的会话对象
        String username = hs.getAttribute("username").toString();//获取存储在会话中的用户名，并将其转换为字符串形式
        String type = request.getParameter("type");//获取请求中的类型参数
        iUserTypeService.insert(new UserType(type, DateUtil.now(), username));//插入新的用户类型信息到数据库中
        response.sendRedirect("userTypeList");
    }


    @RequestMapping("updateUserTypeServlet")
    public void updateUserTypeServlet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession hs = request.getSession();
        String username = hs.getAttribute("username").toString();
        String type = request.getParameter("type");
        String id = request.getParameter("id");
        iUserTypeService.updateUser(new UserType(Integer.parseInt(id), type, DateUtil.now(), username));
        response.sendRedirect("userTypeList");
    }


    @RequestMapping("/DeleteUserTypeServlet")
    public void DeleteUserTypeServlet(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("text/html;charset=utf-8");// 设置响应内容类型为UTF-8
            request.setCharacterEncoding("utf-8");  // 设置请求编码为UTF-8
            int id = Integer.parseInt(request.getParameter("id")); // 获取请求参数"id"并转换为整数
            iUserTypeService.delete(id + "");// 调用业务服务删除用户类型
            response.sendRedirect("userTypeList");  // 重定向到用户类型列表页面
        } catch (Exception e) {
            e.printStackTrace();// 捕获异常并打印堆栈信息
        }
    }
}
